from django.template import RequestContext,loader, Context
from django.shortcuts import render_to_response
from thesocialstockmarket.recommendation.forms import *
from django.contrib.auth.decorators import login_required
from django.http import Http404, HttpResponse, HttpResponseRedirect
from thesocialstockmarket.recommendation.models import *
from thesocialstockmarket.users.models import *
#from django.db.models import Sum



# Create your views here.
def fluctuation(request):
    fluctuations = Fluct.objects.all() 
    return render_to_response('recommendation/fluctuation.html',
                              {'user': request.user,
                               'fluctuations': fluctuations})


def show_fluctuation(request, stock_name):
    try:
        stock = Stock.objects.get(stock_name = stock_name)
    except Stock.DoesNotExist:
        raise Http404
    fluctuation = Fluct.objects.filter(stock = stock ).latest('date')
    rec = FluctRec.objects.all()
    if request.method == 'POST': 
        form = FluctRecForm(request.POST)
        if form.is_valid():
            fluctrec = form.save(commit = False)
            fluctrec.stock = stock
            fluctrec.user = request.user
            fluctrec.fluct= fluctuation
            fluctrec.save()

#            return HttpResponseRedirect('/recommendation/fluctrec.html')
    else:
        form = FluctRecForm()

    try:
        has_rated = FluctRec.objects.filter(user = request.user)
    except FluctRec.DoesNotExist:
        has_rated =  None

     
    return render_to_response('recommendation/fluctuation.html',
            {
             'fluctuation': fluctuation,
             'rec': rec,
             'user': request.user,
             's': stock,
             'form': form,
             'has_rated': has_rated,
             })


# Create your views here.
def fluctrecrating(request,id,stock_name):
    
    try:
        stock = Stock.objects.get(stock_name = stock_name)
    except Stock.DoesNotExist:
        raise Http404
    
    fluctrec = FluctRec.objects.get(id = id)

    fluct_rat = FluctRecRating.objects.filter(fluct_rec = fluctrec)

   
    if request.method == 'POST': 
        form = FluctRatingForm(request.POST)
        if form.is_valid():
            fluctrating = form.save(commit = False)
            fluctrating.user = request.user
            fluctrating.fluct_rec = fluctrec
            fluctrating.save()
    else:
        form = FluctRatingForm()
   
    try:
        has_rated = FluctRecRating.objects.get(fluct_rec = fluctrec, user = request.user)
    except FluctRecRating.DoesNotExist:
        has_rated =  None
  
 #Sum the total rating 
    count = 0
    sum = 0
    avg_rat = 0.0
    for rat in fluct_rat:
        sum += int(rat.rating)
        count += int(rat.fluct_rec_id)
    
        if count is 0:
             avg_rat 
        else:
             avg_rat = float(sum / count)
           
    return render_to_response('recommendation/fluctrecrating.html', 
                               {'user': request.user,
                                's': stock,
                                'fluctrec': fluctrec,
                                'fluct_rat': fluct_rat,
                                'sum': sum,
                                'avg_rat': avg_rat,
                                'count': count,
                                'form': form,
                                'has_rated': has_rated,
                            }) 